<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Action\PageJumpAction;
use App\Admin\Extensions\Form\Course\Edit;
use App\Http\Traits\GlobalConstant;
use App\Models\Campus;
use App\Models\Course;
use App\Models\CourseDuty;
use App\Models\CourseSection;
use App\Models\Mutual;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Tab;
use Encore\Admin\Widgets\Table;

class CourseController extends BaseController
{


    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('课程列表')
            ->description(' ')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('课程详情')
            ->description(' ')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑课程')
            ->description(' ')
            ->body($this->form($id)->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新增课程')
            ->description('新增课程')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Course());
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->column(1 / 2, function ($filter) {
                $filter->like('name', '课程名称');
                $filter->equal('campusInfo.id', '校区')->select(Campus::where('status', 1)->pluck('name as text', 'id'));
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('valid', '状态')->select(['禁用', '正常']);
//                $filter->equal('is_sug', '推荐')->select(GlobalConstant::IS_SUG);
            });

        });
//
        $grid->model()->orderBy('id', 'desc');
        $grid->column('id', 'ID')->sortable();
        $grid->column('name', '名称')->limit(30);
        $grid->column('campusInfo.name', '校区')->sortable();
        $grid->column('teacherInfo.name', '授课老师')->sortable();
        $grid->column('section', '章节')->expand(function ($model) {
            $list = $model->courseSectionInfo()->get()->map(function ($item) {
                return $item->only(['serial_number', 'name', 'class_time', 'begin_time', 'end_time']);
            });
            return new Table(['章节序号', '章节名称', '上课时间', '开始时间', '结束时间'], $list->toArray());
        });
        $grid->column('section_number', '章节数')->display(function () {
            return CourseSection::where('course_id', $this->id)->where('status', 1)->count();
        });

        $grid->column('score', '积分')->sortable();
        $grid->column('sign_percent', '签到比例(%)')->display(function () {
            $duty  = CourseDuty::where('course_id', $this->id)->count();
            $total = CourseSection::where('course_id', $this->id)->where('status', 1)->count();
            if ($total == 0) {
                return 0;
            }
            return sprintf("%.2f",$duty / $total)*100;
        });
        $grid->column('start_time', '开课日期')->sortable();
        $grid->column('class_time', '上课时间');
        $grid->column('class_room', '教室')->limit(10);
        $grid->column('discover', '封面')->lightbox(['width' => 30, 'height' => 30]);
        $grid->column('pics', '图片集')->lightbox(['width' => 30, 'height' => 30]);
//        $grid->column('video_url', '视频')->display(function ($v) {
//            return '复制';
//        })->copyable();
//        $grid->column('price', '价格')->sortable();
        $grid->column('total', '名额');
//        $grid->column('read_num', '浏览量');
//        $grid->column('is_sug', '推荐')->display(function ($v) {
//            return GlobalConstant::IS_SUG[$v];
//        })->label([1 => 'danger', 2 => 'primary'])->sortable();
//        $grid->column('hot', '热度')->sortable();
        $grid->column('valid', __('状态'))->display(function ($v) {
            return GlobalConstant::_STATUS[$v];
        })->label([0 => 'danger', 1 => 'success'])->sortable();
//        $grid->column('created_at', __('created_at'))->sortable();
//        $grid->column('updated_at', __('updated_at'));

        $grid->actions(function ($actions) {
//            $actions->disableDelete();
            $actions->disableView();
            $actions->disableEdit();
            $actions->add(new PageJumpAction("编辑", '/admin/course-edit?c_id=' . $actions->row->id));
        });
        $grid->disableCreateButton();
        $url  = '/admin/course-edit';
        $html = <<<HTML
<div class="btn-group pull-right grid-create-btn" style="margin-right: 10px">
    <a href="$url" class="btn btn-sm btn-success">
        <i class="fa fa-plus"></i>&nbsp;&nbsp;新增课程
    </a>
</div>
HTML;
        $grid->tools->append($html);
        $grid->disableRowSelector();
        $grid->tools(function ($tools) {
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });

        $grid->expandFilter();
        Admin::style('.img-thumbnail {width: 60px;height:60px}');
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Course::findOrFail($id));

        $show->panel()
            ->tools(function ($tools) {
                $tools->disableDelete();
            });
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($id = 0)
    {
        $form = new Form(new Course());
        $form->text('name', '名称')->required()->attribute(['autocomplete' => 'off']);
        $form->select('campus_id', '校区')->options(Campus::where('status', 1)->pluck('name as text', 'id'))->required();
//        $form->number('score','积分')->help('课程基础积分');
        $form->image('discover', '封面')->required()->uniqueName();
        $form->file('video_url', '视频')->uniqueName()->required();
        $form->date('start_time', '开课日期')->required();
        $form->text('class_time', '上课时间')->required();
        $form->text('class_room', '教室')->required();
        $form->currency('price', '价格')->symbol('￥')->required();
        $form->number('total', '名额')->required()->min(0)->value(0);
//        $form->text('intro', '视频标签文字')->required();
//        $form->number('hot', '热度')->default(0);
//        $form->radio('is_sug', '推荐')->options(GlobalConstant::IS_SUG);
        $form->radio('valid', '状态')->options(GlobalConstant::_STATUS);
        $form->multipleImage('pics', '图片集')->uniqueName()->removable();
        $form->text('tips', '提示')->help('如“最终时间以学校通知为准”');
        $form->editor('desc', '课程简介');
        $form->editor('reflex', '报名条件');
        $form->tools(function (Form\Tools $tools) {
            // 去掉`删除`按钮
            $tools->disableDelete();
            $tools->disableView();
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
            $footer->disableReset();
        });
        return $form;
    }


    public function courseEdit(Content $content)
    {
        //tab 配置表单
        $tabMenu = [
            'Course_Edit' => Edit::class,
        ];

        $tab      = Tab::forms($tabMenu);
        $courseId = request('c_id') ?? 0;
        $grid     = new Grid(new CourseSection());
        $grid->model()->where(['course_id' => $courseId]);
        $grid->disablePagination();
        $grid->disableFilter();
        $grid->disableExport();
        $grid->disableRowSelector();
        $grid->disableColumnSelector();
        $grid->disableCreateButton();
        $url  = '/admin/course-section/create?c_id=' . $courseId;
        $html = <<<HTML
<div class="btn-group pull-right grid-create-btn" style="margin-right: 10px">
    <a href="$url" class="btn btn-sm btn-success">
        <i class="fa fa-plus"></i>&nbsp;&nbsp;新增章节
    </a>
</div>
HTML;
        $grid->tools->append($html);
        $grid->column('serial_number', '章节序号')->sortable();
        $grid->column('name', '章节名称')->sortable();
        $grid->column('class_time', '上课时间')->sortable();
        $grid->column('begin_time', '开始时间')->sortable();
        $grid->column('end_time', '结束时间')->sortable();
        $grid->column('status', __('状态'))->display(function ($v) {
            return GlobalConstant::_STATUS[$v];
        })->label([0 => 'danger', 1 => 'success'])->sortable();
        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableView();
            $actions->disableEdit();
            $actions->add(new PageJumpAction("编辑", '/admin/course-section/' . $actions->row->id . '/edit'));
        });
        $tab->add("章节信息", $grid->render());

//        $mutualGrid = app('App\Admin\Controllers\MutualController')->grid();
//        $tab->add("课程交互", $mutualGrid->render());
        return $content
            ->title('课程')
            ->body($tab);
    }

}
